package com.glorysoft.sunnypda.ui.eqpregister

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.databinding.ActivityEqpRegisterBinding
import com.glorysoft.sunnypda.entity.EqpByAreaListEntity
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.ui.autograph.AutographActivity
import com.glorysoft.sunnypda.ui.eqpinfo.EqpInfoActivity
import com.glorysoft.sunnypda.ui.reportrepair.ReportRepairInfoActivity
import com.glorysoft.sunnypda.utils.PopWindowsUtils
import kotlinx.android.synthetic.main.activity_eqp_register.*

/**
 * FileName: EqpRegisterActivity
 * Author: XJH
 * Date: 2022/1/7 17:29
 * Description:
 * History:
 */
class EqpRegisterActivity : BaseActivity<EqpRegisterViewModel,ActivityEqpRegisterBinding>(),
    View.OnClickListener {

    private var fabPop: PopWindowsUtils.GeneralPop? = null
    private var eqpTypePop: PopWindowsUtils.GeneralPop? = null
    private var eqpEntity : EqpInfoEntity? = null

    override fun layoutId(): Int {
        return R.layout.activity_eqp_register
    }

    override fun initVariableId(): Int {
        return BR.eqpRegisterViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.equipment_registration))
        viewModel.getFabList()
        tvAddEqp.setOnClickListener(this)
        flFabList.setOnClickListener(this)
        fleqptypeList.setOnClickListener(this)
        setRightListener(getString(R.string.confirm)){
            if (viewModel.eqpItems.size <= 0){
                viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.add_main_eqp),false)
                return@setRightListener
            }
            val intent = Intent(this, AutographActivity:: class.java)
            startActivityForResult(intent,1000)
        }

        initEdit(etEqpWork,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                viewModel.getEqpInfoAdd(s)
            }

        })
    }

    override fun initData() {
        viewModel.defUI.callObserve.observe(this){
            val eqpBean =  EqpInfoEntity()
            eqpBean.EQPID = etEqpWork.text.toString()
//            eqpBean.FAB = tvFab.text.toString()
            eqpBean.EQPTYPE = tvEqpType.text.toString()
            eqpBean.SHOWNAME = etShowName.text.toString()
            eqpEntity = eqpBean
        }

        viewModel.defUI.startActivity.observe(this){
            val intent = Intent(this, EqpInfoActivity::class.java)
            intent.putExtras(it)
            startActivityForResult(intent,0)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK){
            if (data != null){
                if (requestCode == 1000){
                    val stringExtra = data.getStringExtra("BITMAP")
                    viewModel.submit(this,stringExtra)
                }else{
                    val eqpInfoEntity = data.getSerializableExtra("DATA") as EqpInfoEntity?
                    if(eqpInfoEntity!!.NO == null || eqpInfoEntity.NO == ""){
                        eqpInfoEntity.NO = "0"
                    }
                    viewModel.eqpItems[(eqpInfoEntity.NO!!.toInt() - 1)] = eqpInfoEntity
                }

            }

        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.tvAddEqp ->{
                if (etEqpWork.text.toString().isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.eqp_hint),false)
                    return
                }

//                if (tvFab.text.toString().isEmpty()){
//                    viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.selector_fab),false)
//                    return
//                }

                if (eqpEntity == null){
                    viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.eqp_hint),false)
                    return
                }

                eqpEntity!!.SHOWNAME = etShowName.text.toString()
                eqpEntity!!.EQPTYPE = tvEqpType.text.toString()

                viewModel.addEqpToList(eqpEntity!!)
                etEqpWork.setText("")
//                tvFab.text = ""
                tvEqpType.text = ""
                etShowName.setText("")
                eqpEntity = null
            }
            R.id.flFabList ->{
                if (viewModel.fabList.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.return_area_is_empty),false)
                    return
                }
                if (fabPop == null) {
                    fabPop = PopWindowsUtils.getOneChoosePop(
                        this,
                        flFabList.width,
                        viewModel.fabList,
                        ivFabArrow,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            @RequiresApi(Build.VERSION_CODES.P)
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvFab.text = codeS
                            }

                        })

                }

                if (fabPop!!.isShowing) {
                    fabPop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivFabArrow)
                    fabPop?.showAsDropDown(flFabList, 0, 0)
                }
            }
            R.id.fleqptypeList ->{
                if (viewModel.eqpTypeList.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean(getString(R.string.return_area_is_empty),false)
                    return
                }
                if (eqpTypePop == null) {
                    eqpTypePop = PopWindowsUtils.getOneChoosePop(
                        this,
                        fleqptypeList.width,
                        viewModel.eqpTypeList,
                        ivEqpTypeArrow,
                        object :
                            PopWindowsUtils.PopTextAndPositionCallback {
                            @RequiresApi(Build.VERSION_CODES.P)
                            override fun onSuccess(codeS: String, potion: Int) {
                                tvEqpType.text = codeS
                            }

                        })

                }

                if (eqpTypePop!!.isShowing) {
                    eqpTypePop?.dismiss()
                } else {
                    PopWindowsUtils.setArrowShow(ivEqpTypeArrow)
                    eqpTypePop?.showAsDropDown(fleqptypeList, 0, 0)
                }
            }
        }
    }
}